/**
 * Created by legolas on 2017/10/17 0017.
 */

// 表单转js对象
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

//全局设置error
$(document).ajaxError(function(){
    alert("操作失败!");
});

//  文件上传格式控制
function fileChange(target) {
    var fileSize = 0;
    if (isIE && !target.files) {
        var filePath = target.value;
        var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
        var file = fileSystem.GetFile(filePath);
        fileSize = file.Size;
    } else {
        fileSize = target.files[0].size;
    }
    var size = fileSize / 1024;
    if (size > 20000) {
        alert("附件不能大于20M");
        target.value = "";
    }
    var name = target.value;
    var fileName = name.substring(name.lastIndexOf(".") + 1).toLowerCase();
    if (fileName != "xls") {
        alert("请选择execl格式文件上传！");
        target.value = "";
    }
}

//检测是否是IE
function isIE() {
    var navigatorName = "Microsoft Internet Explorer";
    var isIE = false;
    if (navigator.appName == navigatorName) {
        isIE = true;
    }
    return isIE;
}
//  日期格式转换
function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
//  return y + '-' + (m < 10 ? ('0' + m) : m);
}
function myparser(s) {
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    }
    else {
        return new Date();
    }
}